package com.kgc.ssm.controller;

import com.kgc.ssm.beans.EdocEntry;
import com.kgc.ssm.service.EdocEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * Created on 2021/6/30.
 * <p>
 * Author: 190120
 * <p>
 * Description:电子文档操作入口
 */
@Controller
public class EdocEntryController {

    @Autowired
    private EdocEntryService edocEntryService;

    /**
     *
     */
    @RequestMapping("/edocEntries")
    public String edocEntryList(Map<String,Object> map){

        map.put("edocCategories",edocEntryService.selectAllCategory());
        //调用业务接口,查询所有的电子文档详情
        map.put("edocEntries", edocEntryService.queryEdocEntryList());
        //返回目标视图
        return  "edocList";
    }

    @RequestMapping(value = "/seledocInfo/{cid}",method = RequestMethod.GET)
    public String selectEntry(@PathVariable("cid")Integer cid,Map<String,Object> map){

        map.put("edocCategories",edocEntryService.selectAllCategory());
        //调用业务接口,查询所有的电子文档详情
        //map.put("edocEntries", new EdocEntry());
        map.put("edocEntries", edocEntryService.selectEntryByCid(cid));
        //System.out.println(edocEntryService.selectEntryByCid(cid));

        return  "edocList";
    }

    @RequestMapping(value = "/edocInfo/{id}", method = RequestMethod.DELETE)
    public String delEdocEntry(@PathVariable("id")Integer id){
        // 删除文档
        edocEntryService.deleteEntryById(id);

        // 返回目标视图，rest请求存在多级，保证从项目根目录开始请求，不能少 /
        return "redirect:/edocEntries";
    }

    @RequestMapping(value = "/toAddEdocEntry", method = RequestMethod.GET)
    public String toAddEdocEntry(Map<String, Object> map){
        // 获取分类列表，存入map中
        map.put("edocCategories", edocEntryService.selectAllCategory());

        // 默认情况下，form表单标签，必须添加一个command作为key的模型数据
        // 添加，没有初始数据，直接提供一个空的电子文档实体即可，页面取的值都是空的
        // map.put("command", new EdocEntry());
        // 可以自定义修改表单默认的command参数
        map.put("edocEntry", new EdocEntry());

        // 返回目标视图
        return "edocInfo";
    }

    @RequestMapping(value = "/edocInfo", method = RequestMethod.POST)
    public String addEdocEntry(EdocEntry edocEntry){

        // 添加电子文档实体
        edocEntryService.insOrUpdEdocEntry(edocEntry);

        // 返回目标视图
        return "redirect:edocEntries";
    }

    @RequestMapping(value = "/edocInfo/{id}", method = RequestMethod.GET)
    public String toModEdocEntry(@PathVariable("id")Integer id, Map<String, Object> map){
        // 获取分类列表，存入map中
        map.put("edocCategories", edocEntryService.selectAllCategory());

        // 查询一个文档实体详情，存入模型数据
        // map.put("command", edocEntryMapper.selectEntryById(id));
        map.put("edocEntry", edocEntryService.selectEntryById(id));

        // 返回目标视图
        return "edocInfo";
    }

    @RequestMapping(value = "/edocInfo", method = RequestMethod.PUT)
    public String modEdocEntry(EdocEntry edocEntry){
        // 根据获取的分类编号，获取分类实体
        //edocEntry.setEdocCategory(edocEntryService.selectCategoryById(edocEntry.getEdocCategory().getId()));

        // 添加电子文档实体
        edocEntryService.UpdEdocEntry(edocEntry);

        // 返回目标视图
        return "redirect:edocEntries";
    }







}
